<?php
namespace app\index\controller;

use app\common\model\Group;
use app\common\model\GroupControl;
use app\common\model\GroupUser;
use think\Controller;
use util\Log;

class User extends Controller
{

    /**
     * 初始化用户信息
     *
     * @return void
     */
    public function initUserInfo($myself, $groups, $session_id, $pid)
    {
        try {
            $user = new \app\common\model\User();
            $group = new Group();
            $group_control = new GroupControl();
            $group_user = new GroupUser();
            //处理自己的信息
            //查询自己的数据信息
            $user_where['uin'] = $myself->uin;
            $base_user_map['where'] = $user_where;
            $base_user_map['unique'] = 1;
            $user_info = $user->baseSelect($base_user_map);
            if (!empty($user_info)) {
                //修改
                $update_user_map['username'] = $myself->username;
                $update_user_map['nickname'] = $myself->nickname;
                $update_user_map['sex'] = $myself->sex;
                $update_user_map['online'] = 1;
                $update_user_map['session_id'] = $session_id;
                $update_user_map['pid'] = $pid;
                $update_user_map['id'] = $user_info['id'];
                $user_res = $user->baseEdit($update_user_map);
            } else {
                //新增
                $insert_user_map['uin'] = $myself->uin;
                $insert_user_map['username'] = $myself->username;
                $insert_user_map['nickname'] = $myself->nickname;
                $insert_user_map['sex'] = $myself->sex;
                $insert_user_map['online'] = 1;
                $insert_user_map['session_id'] = $session_id;
                $insert_user_map['pid'] = $pid;
                $user_res = $user->baseInsert($insert_user_map);
            }

            //处理组信息
            //删除所有当前用户的组信息
            if (!empty($groups)) {
                foreach ($groups as $k => $v) {
                    //查询该群聊记录是否被记录到数据库 存在就是修改 不存在就是新增 根据uin跟群聊名称进行唯一确认
                    //1.查询记录是否存在
                    $tmp_group_where['uin'] = $myself->uin;
                    $tmp_group_where['nickname'] = $v['NickName'];
                    $base_tmp_group_where['where'] = $tmp_group_where;
                    $base_tmp_group_where['unique'] = 1;
                    $tmp_group_info = $group->baseSelect($base_tmp_group_where);
                    if (!empty($tmp_group_info)) {
                        //修改
                        $update_group = new Group();
                        $update_group_map['username'] = $v['UserName'];
                        $update_group_map['nickname'] = $v['NickName'];
                        $update_group_map['count'] = count($v['MemberList']);
                        $update_group_map['chart_room_id'] = $v['ChatRoomId'];
                        $update_group_map['id'] = $tmp_group_info['id'];
                        $update_group->baseEdit($update_group_map);
                        //判断该群聊是否需要监控
                        $group_control_where['uin'] = $myself->uin;
                        $group_control_where['group_name'] = $v['NickName'];
                        $base_group_control_map['where'] = $group_control_where;
                        $base_group_control_map['unique'] = 1;
                        $group_control_info = $group_control->baseSelect($base_group_control_map);
                        if (!empty($group_control_info)) {
                            // $group_user_delete_map['uin'] = $myself->uin;
                            // $group_user_delete_map['group_name'] = $v['NickName'];
                            // $group_user->where($group_user_delete_map)->delete();
                            $group_user->baseDelete($group_control_info['group_id'], 'group_id');
                            //将成员全部记录到数据库中
                            Log::info($v['NickName'] . '该群需要监控');
                            foreach ($v['MemberList'] as $m => $s) {
                                $add_group_user = new GroupUser();
                                $add_group_user_map['uin'] = $myself->uin;
                                $add_group_user_map['group_id'] = $group_control_info['group_id'];
                                $add_group_user_map['group_name'] = $v['NickName'];
                                $add_group_user_map['username'] = $s['UserName'];
                                $add_group_user_map['nickname'] = $s['NickName'];
                                $add_group_user->baseInsert($add_group_user_map);
                            }
                        }
                    } else {
                        //新增
                        $add_group = new Group();
                        $add_group_map['uin'] = $myself->uin;
                        $add_group_map['username'] = $v['UserName'];
                        $add_group_map['nickname'] = $v['NickName'];
                        $add_group_map['count'] = count($v['MemberList']);
                        $add_group_map['chart_room_id'] = $v['ChatRoomId'];
                        $add_group->baseInsert($add_group_map);
                    }
                }
            }
            return $user_res;
        } catch (\Exception $e) {
            Log::error('调用方法出现错误，位置 index/controller/User/initUserInfo,出错原因:' . $e->getMessage());
        }
    }

    /**
     * 查看当前监控的聊天群是否有人进入或退出
     *
     * @param [type] $message
     * @return void
     */
    public function changeGroup($message)
    {
        try {

            $group = new Group();
            $group_user = new GroupUser();
            $group_control = new GroupControl();
            $result = array();
            $group_where['username'] = $message['from']['UserName'];
            $base_group_map['where'] = $group_where;
            $base_group_map['unique'] = 1;
            $group_info = $group->baseSelect($base_group_map);

            $group_control_where['group_id'] = $group_info['id'];
            $base_group_control_map['where'] = $group_control_where;
            $base_group_control_map['unique'] = 1;
            $group_control_info = $group_control->baseSelect($base_group_control_map);

            if (empty($group_control_info)) {
                Log::info('该聊天组不需要监控');
                return -2;
            }
            //Log::info('group_info' . var_export($group_info, true));
            //有人离开了群聊
            if ($group_info['count'] > $message['from']['MemberCount']) {
                $group_user_where['group_id'] = $group_info['id'];
                $base_group_user_map['where'] = $group_user_where;
                $group_user_list = $group_user->baseSelect($base_group_user_map);
                //Log::info('group_user_list' . var_export($group_user_list, true));
                foreach ($group_user_list as $k => $v) {
                    $x = 0;
                    foreach ($message['from']['MemberList'] as $s) {
                        if ($v['username'] == $s['UserName']) {
                            //  Log::info('username' . $s['UserName']);
                            $x = 1;
                            break;
                        }
                    }
                    Log::info('username' . $v['username'] . '  x ' . $x);
                    if ($x == 0) {
                        //  Log::info('删除的对象' . $v['username']);
                        //删除当前不存在的用户
                        $group_user->baseDelete($v['id']);
                        //修改聊天群人数
                        $update_group_map['count'] = $group_info['count'] - 1;
                        $update_group_map['id'] = $group_info['id'];
                        $group->baseEdit($update_group_map);
                        $v['type'] = 'remove';
                        $result = $v;
                    }
                }
                return $result;
            }
            //进来了新的用户
            else if ($group_info['count'] < $message['from']['MemberCount']) {
                $group_user_where['group_id'] = $group_info['id'];
                $base_group_user_map['where'] = $group_user_where;
                $group_user_list = $group_user->baseSelect($base_group_user_map);
                //Log::info('group_user_list' . var_export($group_user_list, true));
                foreach ($message['from']['MemberList'] as $k => $v) {
                    $x = 0;
                    foreach ($group_user_list as $s) {
                        if ($v['UserName'] == $s['username']) {
                            $x = 1;
                            break;
                        }
                    }
                    Log::info('username' . $v['UserName'] . '  x ' . $x);
                    if ($x == 0) {
                        //  Log::info('删除的对象' . $v['username']);
                        //增加当前不存在的用户
                        $group_user_insert['uin'] = $group_info['uin'];
                        $group_user_insert['group_id'] = $group_info['id'];
                        $group_user_insert['group_name'] = $group_info['nickname'];
                        $group_user_insert['username'] = $v['UserName'];
                        $group_user_insert['nickname'] = $v['NickName'];
                        $group_user->baseInsert($group_user_insert);
                        //修改聊天群人数
                        $update_group_map['count'] = $group_info['count'] + 1;
                        $update_group_map['id'] = $group_info['id'];
                        $group->baseEdit($update_group_map);
                        $v['type'] = 'add';
                        $result = $v;
                        break;
                    }
                }
                return $result;
            } else {
                return -1;
            }

        } catch (\Exception $e) {
            Log::error('调用方法出现错误，位置 index/controller/User/changeGroup,出错原因:' . $e->getMessage());
        }

    }

    /**
     * 修改群监控信息
     *
     * @return void
     */
    public function changeGroupControl($uin, $groups)
    {
        $group_control = new GroupControl();
        $group_user = new GroupUser();
        foreach ($groups as $k => $v) {
            //判断该群聊是否需要监控
            $group_control_where['uin'] = $uin;
            $group_control_where['group_name'] = $v['NickName'];
            $base_group_control_map['where'] = $group_control_where;
            $base_group_control_map['unique'] = 1;
            $group_control_info = $group_control->baseSelect($base_group_control_map);
            if (!empty($group_control_info)) {
                // $group_user_delete_map['uin'] = $myself->uin;
                // $group_user_delete_map['group_name'] = $v['NickName'];
                // $group_user->where($group_user_delete_map)->delete();
                $group_user->baseDelete($group_control_info['group_id'], 'group_id');
                //将成员全部记录到数据库中
                Log::info($v['NickName'] . '该群需要监控');
                foreach ($v['MemberList'] as $m => $s) {
                    $add_group_user = new GroupUser();
                    $add_group_user_map['uin'] = $uin;
                    $add_group_user_map['group_id'] = $group_control_info['group_id'];
                    $add_group_user_map['group_name'] = $v['NickName'];
                    $add_group_user_map['username'] = $s['UserName'];
                    $add_group_user_map['nickname'] = $s['NickName'];
                    $add_group_user->baseInsert($add_group_user_map);
                }
            }
        }
    }

    /**
     * 聊天群变动信息
     *
     * @return void
     */
    public function group_change_count($message, $type)
    {
        $group = new Group();
        $group_user = new GroupUser();
        $group_control = new GroupControl();
        $result = array();
        $group_where['username'] = $message['from']['UserName'];
        $base_group_map['where'] = $group_where;
        $base_group_map['unique'] = 1;
        $group_info = $group->baseSelect($base_group_map);
        $group_control_where['group_id'] = $group_info['id'];
        $base_group_control_map['where'] = $group_control_where;
        $base_group_control_map['unique'] = 1;
        $group_control_info = $group_control->baseSelect($base_group_control_map);
        if (empty($group_control_info)) {
            Log::info('group_change该聊天组没有监控');
            return -2;
        }
        if ($type == 'add') {
            //新增
            $update_group_map['count'] = $group_info['count'] + 1;
            $update_group_map['id'] = $group_info['id'];
            $group->baseEdit($update_group_map);
            return 1;
        } else if ($type == 'remove') {
            //移出
            $update_group_map['count'] = $group_info['count'] - 1;
            $update_group_map['id'] = $group_info['id'];
            $group->baseEdit($update_group_map);
            return 1;
        }
    }

}
